import Style from "./index.module.scss";
import { Button, Checkbox, Form, Input, message } from "antd";
import type { FormProps } from "antd";
import classNames from "classnames";
import { redirect, Link, useNavigate } from "react-router-dom";
import { useEffect } from "react";
import request from "@/utils/request";
import { api } from "@/api";
import axios from "axios";
import { useSetToken } from "@/hooks/useHandToken";
import { useDispatch, useSelector } from "react-redux";
import { useReducer } from "@/store/user";
import { setUserInfo } from "@/store/user";
import user from "../../api/user";
type FieldType = {
  username?: string;
  password?: string;
  remember?: string;
  phone?: string;
};

const Login = () => {
  const navigate = useNavigate();
  const dispatch = useDispatch();
  const onLogin = async () => {
    console.log(api);
    const res = await api.user.postLoginApi();
    console.log(res);
    if (res.code === 200) {
      message.success("登录成功");
      dispatch(setUserInfo(res.data));
      useSetToken(res.data.token);
      navigate("/");
    }
  };
  const onFinish: FormProps<FieldType>["onFinish"] = async (values) => {
    console.log("Success:", values);
    const res = await api.user.postLoginApi({
      phone: values.phone,
      password: values.password,
    });
    console.log(res);
    if (res.code === 200) {
      message.success("登录成功");
      useSetToken(res.data.token);
      dispatch(setUserInfo(res.data));
      navigate("/");
    }
  };
  useEffect(() => {}, []);
  return (
    <div className={Style.loginBox}>
      <Form
        name="basic"
        labelCol={{ span: 8 }}
        wrapperCol={{ span: 16 }}
        style={{ maxWidth: 600 }}
        initialValues={{ remember: true }}
        onFinish={onFinish}
        autoComplete="off"
      >
        <Form.Item<FieldType>
          label="phone"
          name="phone"
          rules={[{ required: true, message: "请填写您的电话号码" }]}
        >
          <Input />
        </Form.Item>

        <Form.Item<FieldType>
          label="Password"
          name="password"
          rules={[{ required: true, message: "请输入您的密码" }]}
        >
          <Input.Password />
        </Form.Item>

        <Form.Item<FieldType>
          name="remember"
          valuePropName="checked"
          label={null}
        >
          <div>
            <Checkbox>记住我</Checkbox>
            <Link to={"/register"}>
              <span className={classNames([Style.registerText, Style.cursor])}>
                没有账号？点击注册
              </span>
            </Link>
          </div>
        </Form.Item>

        <Form.Item label={null}>
          <Button type="primary" htmlType="submit">
            登录
          </Button>
        </Form.Item>
      </Form>
    </div>
  );
};
export default Login;
